package com.zx.viewlearn.views;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.zx.viewlearn.R;

/**
 * Created by zhoux on 2018/1/8.
 * 注释：自定义view Shader学习
 */

public class MyView7 extends AppCompatTextView {
    private String TAG = "--------MyView7";
    private int width, height;
    private int mTranslate;

    Paint mPaint;
    Paint mPaint2;

    public MyView7(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.i(TAG, "MyView7()");
        init();
    }

    public MyView7(Context context) {
        super(context);
        Log.i(TAG, "MyView7()");
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(getResources().getColor(R.color.colorAccent));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(3F);
        mPaint2 = new Paint();
        mPaint2.setColor(getResources().getColor(R.color.blue));
        mPaint2.setStyle(Paint.Style.STROKE);
        mPaint2.setStrokeWidth(3F);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.i(TAG, "onFinishInflate()");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i(TAG, "onMeasure()");
    }

    LinearGradient mLinearGradient;//todo 渐变色
    Matrix mMatrix;//todo 位移

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
//        width = w;
//        height = h;
        Log.i(TAG, "onSizeChanged()");

        if (width == 0) {
            width = getMeasuredWidth();
            if (width > 0) {
                mPaint = getPaint();//todo 获取文字的Paint
                mLinearGradient = new LinearGradient(0, 0, width, 0, new int[]{Color.BLUE, Color.RED, Color.YELLOW, 0xffffffff, Color.DKGRAY, Color.MAGENTA}, null, Shader.TileMode.CLAMP);//渐变色
                mPaint.setShader(mLinearGradient);//todo 填充图形
                mMatrix = new Matrix();
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.i(TAG, "onLayout()");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG, "onDraw()");
//        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);//外层
//        canvas.drawRect(10, 10, getMeasuredWidth() - 10, getMeasuredHeight() - 10, mPaint2);//内层
//        canvas.save();//开始移动坐标系，保存当前状态
//        canvas.translate(10, 10);//移动坐标系，最好放在save和restore里面
//        super.onDraw(canvas);
//        canvas.restore();//还原保存时候的状态
//
//
// TODO: 2018/1/8 动起来
        if (mLinearGradient != null) {
            mTranslate += width / 5;
            if (mTranslate > 2 * width) {
                mTranslate = -width;
            }
            mMatrix.setTranslate(mTranslate, 0);//x轴位移mTranslate距离
            mLinearGradient.setLocalMatrix(mMatrix);
            postInvalidateDelayed(100);
        }
    }


}
